package com.xiaoqi.ajax.service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
@WebServlet("/doBMI")
public class AjaxBMI extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String parameter = req.getParameter("username");
        String age = req.getParameter("age");
        String weight = req.getParameter("weight");
//        System.out.println(parameter);
        System.out.println("age=="+age);
        System.out.println("weight=="+weight);
        String msg="";
        int result=0;
        if (age!=null && weight!=null){
            int peoperAge = Integer.valueOf(age);
            int peopreWeight = Integer.valueOf(weight);
            if (peoperAge>0 && peopreWeight>0){
                result=(int) peopreWeight*peopreWeight/peoperAge;
            }
        }
        msg="用户您好，您的BMI系数为"+result;
        responseClient(msg,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
    public void responseClient(String msg,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        System.out.println(msg);
        writer.print(msg);
    }
}
